// 时间处理模块

var now = new Date()
var year1 = now.getFullYear()
now.setHours(0, 0, 0, 0)
var week = now.getDay()
now = now.getTime()
//将时间变为两位数模式
function checkime(time){
	return time<10?'0'+time:time
} 
//聊天界面时间格式化
export function dataformate(time){
	 time = new Date(time)
	var year = time.getFullYear()
	var month = checkime(time.getMonth()+1)
	var day = checkime(time.getDate())
	var hour = checkime(time.getHours())
	var minutes = checkime(time.getMinutes())
	if(now<=time-0){
		return hour +':'+minutes
	}else if(now-time<= 86400000){
			return "昨天 " + hour + ":" + minutes
	}else if(year1!=year){
		return year + '年'+month+'月'+day +"日"
	}
	else{
		return year + '年'+month+'月'+day +"日  "+hour+':'+minutes
	}
}
//时间间隔处理，一分钟内聊天记录合并
export function spacTime(a){
	var arr = a
	arr[0].ontime = true
	for(let i = 0 ; i<arr.length-1;i++){
		if(new Date(arr[i+1].time)-new Date(arr[i].time)<=60*1000){
			arr[i+1].ontime = false
		}else{
			arr[i+1].ontime = true
		}
	} 
	return arr
}
//时间处理为年-月-日格式
export function ymd(time){
	time = new Date(time)
	var year = time.getFullYear()
	var mouth = time.getMonth()+1
	var day = time.getDate()
	return `${year}-${mouth}-${day}`
}

//首页时间格式化
export default function(time) {
	var time1 = new Date(time)
	var time2 = time1.getTime()
	var year = checkime(time1.getFullYear())
	var month = checkime(time1.getMonth() + 1)
	var day = checkime(time1.getDate())
	var hour = checkime(time1.getHours())
	var minutes = checkime(time1.getMinutes())
	if (week == 0) {
		week = 7
	}
	if (time2 >= now) {
		return hour + ":" + minutes
	}
	if (now - time2 <= 86400000) {
		return "昨天" + hour + ":" + minutes
	} else if (now - time2 <= 86400000 * week) {
		if (time1.getDay() == 1) {
			return "星期一"
		}
		if (time1.getDay() == 2) {
			return "星期二"
		}
		if (time1.getDay() == 3) {
			return "星期三"
		}
		if (time1.getDay() == 4) {
			return "星期四"
		}
		if (time1.getDay() == 5) {
			return "星期五"
		}
		if (time1.getDay() == 16) {
			return "星期六"
		}
		if (time1.getDay() == 0) {
			return "星期日"
		}
	}
	return year + '-' + month + "-" + day
}

